package com.lz.fruitday.ui;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;
import com.lz.fruitday.R;
import com.lz.fruitday.adapter.QXAdapter;
import com.lz.fruitday.bean.QiangxianBaseBean;
import com.lz.fruitday.bean.QiangxianProduct;
import com.lz.fruitday.constant.Path;
import com.lz.fruitday.utils.ImageLoaderUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

public class QiangxianActivity extends AppCompatActivity {

    @ViewInject(R.id.iv_page_photo)
    private ImageView ivPagePhoto;
    @ViewInject(R.id.lv_qx)
    private ListView lvQx;
    private ArrayList<QiangxianProduct> products = new ArrayList<>();
    private QXAdapter qxAdapter;
    private DisplayImageOptions options = ImageLoaderUtil.getDefaultOptions();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qiangxian);
        ViewUtils.inject(this);
        initView();
        inirData();
    }

    private void inirData() {
        AsyncHttpClient client = new AsyncHttpClient();
        client.post(Path.PATH_FOOD, getParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {

            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, String responseString) {
                TypeReference<QiangxianBaseBean<QiangxianProduct>> typeReference = new TypeReference<QiangxianBaseBean<QiangxianProduct>>() {
                };
                QiangxianBaseBean baseBean = JSONObject.parseObject(responseString, typeReference);
                ImageLoader.getInstance().displayImage(baseBean.getPage_photo(), ivPagePhoto, options);
                products.addAll(baseBean.getProducts());
                qxAdapter.notifyDataSetChanged();
            }
        });
    }

    private RequestParams getParams() {
        RequestParams params = new RequestParams();
        params.add("sign", "30dfebdb1a655fe491ed717ce7ece724");
        params.add("timestamp", "1438873726");
        params.add("platform", "ANDROID");
        params.add("sort", "0");
        params.add("source", "app");
        params.add("connect_id", "");
        params.add("region_id", "143949");
        params.add("service", "product.pageListProducts");
        params.add("page_type", "7");
        params.add("device_id", "93789e8978f2eefdcf52006b4d96ee23");
        params.add("channel", "qq");
        params.add("target_id", "186");
        params.add("version", "2.3.0");
        return params;
    }

    private void initView() {
        qxAdapter = new QXAdapter(products, this);
        lvQx.setAdapter(qxAdapter);
    }


}
